<?php

class slideController extends Controller
{

    public $initphp_list = array(
        'edit',
        'save',
        'uploadimg',
        'del'
    );

    /**
     *
     * @var slideService
     */
    private $slideService;

    /**
     *
     * @var catgoryService
     */
    private $catgoryService;

    public function __construct()
    {
        parent::__construct();
        $this->checkLogin();
        $this->slideService = InitPHP::getService("slide");
    }

    public function run()
    {
        $data = $this->slideService->getAll();
        $this->view->assign("menu_now", "slide");
        $this->view->assign("data", $data);
        $this->view->set_tpl("manage/header", "F"); // 设置公共头部模板
        $this->view->set_tpl("manage/slide/list");
        $this->view->set_tpl("manage/footer", 'L'); // 设置公共尾部模板
        $this->view->assign("title", "文章管理-凯巴斯-管理后台");
        $this->view->display();
    }

    public function edit()
    {
        $id = $this->controller->get_gp("id");
        if ($id > 0) {
            $data = $this->slideService->getOne($id);
            $this->view->assign("id", $id);
            $this->view->assign("data", $data);
        }
        $this->view->assign("menu_now", "slide");
        $this->view->set_tpl("manage/header", "F"); // 设置公共头部模板
        $this->view->set_tpl("manage/slide/add");
        $this->view->set_tpl("manage/footer", 'L'); // 设置公共尾部模板
        $this->view->assign("title", "添加导航-凯巴斯-管理后台");
        $this->view->display();
    }

    public function del()
    {
        $id = $this->controller->get_gp("id");
        $this->slideService->delete($id);
        JsMessage("删除成功！", "/manage/slide/run");
    }

    public function save()
    {
        $id = $this->controller->get_gp("id");
        $data = $this->controller->get_gp(array(
            'pic',
            'link',
            'sort'
        ));
        if ($id) {
            $this->slideService->update($id, $data);
        } else {
            $this->slideService->add($data);
        }
        JsMessage("保存成功！", '/manage/slide/run');
    }

    public function uploadimg()
    {
        $appid = $this->renderArray['appid'];
        $platform = $this->renderArray['platform'];
        $path = APP_PATH . "../upload/images";
        if (! is_dir($path)) {
            _mkdir($path);
        }
        $new_name = "img_" . uniqid() . rand(1, 1000);
        $opinion = array(
            'maxSize' => 2048,
            'allowFileType' => array(
                'gif',
                'jpg',
                'png',
                'bmp'
            )
        );
        $upload = self::getUpload();
        /* 参数：表单名称；上传后新的文件名；存放的文件目录；配置，包含最大文件和文件类型 */
        $result = $upload->upload('upload', $new_name, $path, $opinion);
        if ($result['error'] == 0) {
            echo json_encode(array(
                'status' => 1,
                'data' => "/upload/images/" . $new_name . "." . $result['type']
            ));
            exit();
        } else {
            $this->error($result['msg'], '', true);
        }
    }

    /**
     *
     * @return uploadInit
     */
    private function getUpload()
    {
        return $this->getLibrary('upload'); // 文件上传类加载
    }

    /**
     * 检查登陆状态
     */
    public function checkLogin()
    {
        $session = $this->getUtil('session');
        $adminid = $session->get('adminid');
        if (empty($adminid)) {
            $function = $this->getLibrary('function');
            $function->goto_url('/manage/login');
            die();
        }
        $this->view->assign("adminname", $session->get('adminname'));
        $this->view->assign("adminid", $session->get('adminid'));
    }
}